package com.turbo.cloud.core.base;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.function.Supplier;

/**
 * @author zhangluning
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
@ToString(callSuper = true)
public class PageQuery<T extends Serializable> extends Page<T> {

    /**
     * 查询参数
     */
    private T param;

    public <E> Page<E> toPage() {
        Page<E> page = new Page<>();
        BeanUtils.copyProperties(this, page);
        return page;
    }

    /**
     * 转换参数
     *
     * @param param          目标实体
     * @param targetSupplier 转换实体
     */
    public <E extends Serializable> PageQuery<E> convertParam(T param, Supplier<E> targetSupplier) {
        PageQuery<E> pageQuery = new PageQuery<>();

        E target = targetSupplier.get();
        BeanUtils.copyProperties(param, target);

        pageQuery.setParam(target);
        return pageQuery;
    }
}
